﻿using System;
using System.Web;
using CTS.Com.Domain.Enum;
using CTS.Com.Domain.Exceptions;
using CTS.Com.Domain.Helper;
using CTS.Com.Domain.Logic;
using CTS.Com.Domain.Model;
using CTS.Web.Com.Domain.Constants;

namespace CTS.Web.Com.Domain.Helper
{
    public static class PageHelper
    {
        public static T Invoke<T>(IOperateLogic<T> logic, object obj) where T : BasicResponse, new()
        {
            // Local variable declaration
            T viewModel = new T() { ResultFlag = false };
            try {
                // Copy BasicInfo
                DataHelper.SetProperty(obj, "LocaleCd", WebContextHelper.LocaleCd);
                // Validate
                ValidateHelper.Validate(obj);

                // Invoke processing
                viewModel = (T)logic.Invoke(obj);

                // Set message
                if (viewModel.HasMessage)
                    SetMessage(viewModel.MessageSingle);

                viewModel.ResultFlag = true;
            } catch (SysIgnoreException) {
                viewModel.ResultFlag = true;
                return viewModel;
            } catch (SysRuntimeException ex) {
                // Set message
                SetMessage(ex.Message);
                return viewModel;
            } catch (Exception ex) {
                // Set message
                HttpContext.Current.Session["err-content"] = ex.Message;
                HttpContext.Current.Response.Redirect(Pages.ERROR_PAGE);
                return viewModel;
            }

            // Return value
            return viewModel;
        }

        public static T Invoke<T>(IOperateLogic<T> logic, object param, Type paramType) where T : BasicResponse, new()
        {
            // Local variable declaration
            T viewModel = new T() { ResultFlag = false };
            try {
                // Copy BasicInfo
                DataHelper.SetProperty(param, "LocaleCd", WebContextHelper.LocaleCd);
                // Validate
                var obj = DataHelper.CreateObject(paramType);
                DataHelper.CopyObject(param, obj);
                ValidateHelper.Validate(obj);

                // Invoke processing
                viewModel = (T)logic.Invoke(param);

                // Set message
                if (viewModel.HasMessage)
                    SetMessage(viewModel.MessageSingle);

                viewModel.ResultFlag = true;
            } catch (SysIgnoreException) {
                viewModel.ResultFlag = true;
                return viewModel;
            } catch (SysRuntimeException ex) {
                // Set message
                SetMessage(ex.Message);
                return viewModel;
            } catch (Exception ex) {
                // Set message
                HttpContext.Current.Session["err-content"] = ex.Message;
                HttpContext.Current.Response.Redirect(Pages.ERROR_PAGE);
                return viewModel;
            }

            // Return value
            return viewModel;
        }

        public static void SetMessage(Message msg)
        {
            // Check null
            if (msg == null) return;

            // Add message to session
            switch (msg.MessageType) {
                case MessageType.Fatal:
                    HttpContext.Current.Session["err-content"] = msg.MessageDetail;
                    HttpContext.Current.Response.Redirect(Pages.ERROR_PAGE);
                    break;
                case MessageType.Error:
                    HttpContext.Current.Session["error"] = msg.MessageDetail;
                    break;
                case MessageType.Warn:
                    HttpContext.Current.Session["warn"] = msg.MessageDetail;
                    break;
                case MessageType.Info:
                    HttpContext.Current.Session["info"] = msg.MessageDetail;
                    break;
                default:
                    break;
            }
        }

        public static void LoadUserContext(object context)
        {
            var obj = new UserContext();
            DataHelper.CopyObject(context, obj);
            WebContextHelper.UserContext = obj;
        }

        public static string MapPath(string path)
        {
            return HttpContext.Current.Server.MapPath(path);
        }
    }
}
